import { UserInfo } from "@/users";
import axios from "axios";

export * from "./types/Project";
export * from "./types/Result";
export * from "./types/Part";

import { Project } from "./types/Project";
import { String } from "@/utils/string";
import { Result } from "./types/Result";

const url = process.env.DEVICE_OPS_SERVICE_BASE_URL;

export async function GetProjectAsync(userInfo: UserInfo, projectName: string) {
  const response = await axios.get<Project>(
    url +
      "/api/device-maintenance/projects?" +
      String.getQueryString({ skipCount: 0, maxResultCount: 50 }),
    {
      headers: {
        Authorization:
          userInfo.tokenInfo.token_type + " " + userInfo.tokenInfo.access_token,
      },
    }
  );

  const result = response.data as unknown as Result<Project[]>;

  if (result.isSuccess) {
    const targetProject = result.result.find(
      (item) => item.projectName === projectName
    );
    if (targetProject === null) {
      throw `项目 ${projectName} 不存在！`;
    }
  } else {
    throw result.message;
  }
}
